/**
 * File Name	: BaseEntityDaoImpl.java
 * Author		: adelwin
 * Create Date	: 14-Apr-2011:PM 09:07:56
 *
 * Copyright (c) 2011 adelwin. All Rights Reserved. <BR>
 * <BR>
 * This software contains confidential and proprietary information of
 * Adelwin. ("Confidential Information").<BR>
 * <BR>
 * Such Confidential Information shall not be disclosed and it shall
 * only be used in accordance with the terms of the license agreement
 * entered into with Solveware Independent; other than in accordance with the written
 * permission of Solveware Independent. <BR>
 *
 */
package com.uobgroup.cmt.base.dao.impl;

import java.util.List;

import org.springframework.jdbc.core.RowCallbackHandler;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.jdbc.core.support.JdbcDaoSupport;

import com.uobgroup.cmt.base.dao.IBaseEntityDao;
import com.uobgroup.cmt.base.exception.BaseException;
import com.uobgroup.cmt.base.model.BaseModel;
import com.uobgroup.cmt.base.query.BaseQuery;

/**
 * @author adelwin
 *
 */
public class BaseEntityDaoImpl<M extends BaseModel> extends JdbcDaoSupport implements IBaseEntityDao<M> {

	private BaseQuery query;
	private RowMapper<M> rowMapper;
	private RowCallbackHandler rowCallbackhandler;
	
	@Override
	public RowCallbackHandler getRowCallbackhandler() {
		return rowCallbackhandler;
	}

	@Override
	public void setRowCallbackhandler(RowCallbackHandler rowCallbackhandler) {
		this.rowCallbackhandler = rowCallbackhandler;
	}

	public BaseQuery getQuery() {
		return query;
	}

	public void setQuery(BaseQuery query) {
		this.query = query;
	}

	public RowMapper<M> getRowMapper() {
		return rowMapper;
	}

	public void setRowMapper(RowMapper<M> rowMapper) {
		this.rowMapper = rowMapper;
	}
	
	@Override
	public final void executeForRow(BaseQuery query, RowCallbackHandler callbackHandler) throws BaseException {
		if (this.rowCallbackhandler == null) {
			throw new BaseException("Query for Row was executed, but Row Callback Handler was not defined");
		}
		this.getJdbcTemplate().query(query.getQuery(), callbackHandler);
	}
	
	public final void executeForRow() throws BaseException {
		this.executeForRow(this.query, this.rowCallbackhandler);
	}
	
	@Override
	public final List<M> executeForList(BaseQuery query, RowMapper<M> rowMapper) throws BaseException {
		if (this.rowMapper == null) {
			throw new BaseException("Query for List was executed, but RowMapper was not defined");
		}
		return this.getJdbcTemplate().query(query.getQuery(), rowMapper);
	}
	
	public final List<M> executeForList() throws BaseException {
		return this.executeForList(this.query, this.rowMapper);
	}

	@Override
	public void insert(M model) throws BaseException {
		// TODO Auto-generated method stub
	}


}
